$(function() {
	var service = new EbeimService();
	var user = plugin.cookieToJson(document.cookie);
	var data1, data2;
	var jusername = $("#j-username"), jrole = $("#j-role");
	
	var jform = $("#j-form"),
    jsubmit = $("#j-submit");
	
	//占位符初始化
    plugin.placeHolder({
        con: $(".j-placeHolder")
    });
    
  //表单验证初始化
    jform.find(".j-formcheck").bind('focus',function() {
        var t = $(this);
        var error = t.siblings('.error');
        error.hide();
    });

	jusername.html(user.username);
	if (user.role == "SYSADMIN") {
		jrole.html("系统管理员");
	} else if (user.role == "ADMIN") {
		jrole.html("管理员");
	} else {
		jrole.html("普通用户");
	}
	
	$(".lock-btn").click(function(){
		$(".lock-block").css("display","none");
		$(".reset-form").css("display","block");
	})
	
	$(".j-backLock").click(function(){
		$(".reset-form").css("display","none");
		jform.find("[data-name='password']").val("");
		$(".holder").css("display","block");
		$(".lock-block").css("display","block");
		
	})
	
	//表单提交
    jform.bind("submit", function(){
        if (formCheck(jform)) {
            jsubmit.attr("disabled", "disabled");
            
            var data = jform.serialize();
            data = data + "&username="+ user.username ;
            
            data = plugin.formToJson(data);

            service.unlock(data, function(json){
                if(json.code == 200) {
                	plugin.msgtip({type: "o",msg: "解锁成功！"});
                	setTimeout(function(){
                		window.location.href = "../html/index.html";
                    }, 1500);
                }else{
                	jsubmit.removeAttr("disabled");
                	plugin.msgtip({type: "i",msg: "解锁失败！"});
                }
            },function(){
            	jsubmit.removeAttr("disabled");
            });
        }
        return false;
    });

})

// 表单验证
var formCheck = function(form,name) {

    // 显示错误信息
    var showError = function(obj, msg) {
        var error = obj.siblings('.error');
        error.show();
    };

    // 隐藏错误信息
    var hideError = function(obj) {
        var error = obj.siblings('.error');
        error.hide();
    };

    // 数据验证对象
    var dataCheck = {
        password: {
            obj: form.find("[data-name='password']"),
            fn: function() {
                var val = this.obj.val();
                if (plugin.checkFn.isNull(val)) {
                    showError(this.obj);
                    return false;
                } else {
                    hideError(this.obj);
                    return true;
                }
            }
        }
    };

    if (name && dataCheck[name]) {
        return dataCheck[name].fn();
    } else {
        var errLen = 0;
        for (var i in dataCheck) {
            if (!dataCheck[i].fn()) {
                errLen++;
            }
        }
        if (errLen > 0) {
            return false;
        }
        return true;
    }
}